package me.raih.controller;

import me.raih.pojo.vo.VerifyCode;
import me.raih.util.VerifyCodeGen;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Raih
 * @Time 2021/2/10  7:52
 *
 * 验证码 控制器
 */
@Controller
public class CheckCodeController {
    @RequestMapping(value = "/verifyCode")
    public void verifyCodeGen(HttpServletRequest request, HttpServletResponse response) {

        VerifyCodeGen iVerifyCodeGen = new VerifyCodeGen();

        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            String code = verifyCode.getCode();
            //将VerifyCode绑定session
            request.getSession().setAttribute("VERIFY_CODEGEN", code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
